<?php
function mb_substr1($str,$len)
{
	$s=csubstr($str,0,$len);
	return $s;
}

function mb_substr2($str,$len)
{
	$s=cut_str($str, $len) ;
	return $s;
}

function put_cache($file,$a){
	$str="<?php
return array(";
	foreach ($a as $k=>$v){
		if (is_array($v)) $v=array_str($v);
		else $v="'$v'";
		$str.="
'$k'=>$v,";
	}	
	$str.="
)
?>";
	file_put_contents($file,$str);
}

function array_str($a){
	if (!is_array($a)) return false;
	$str="array(";
	foreach ($a as $k=>$v){
		if (is_array($v)) $v=array_str($v);
		else $v="'$v'";
		$str.="
'$k'=>$v,";
	}
	$str.="
)";
	return $str;
}

function csubstr($str,$start,$len)//截取字符串
{ 
	$strlen=strlen($str); 
	$clen=0; 
	$flag=0;
	for($i=0;$i<$strlen;$i++,$clen++) 
	{ 
		if ($clen>=$start+$len) 
		break; 
		if(ord(substr($str,$i,1))>0xa0) 
		{ 
			if ($clen>=$start) 
			$tmpstr.=substr($str,$i,2); 
			$i++; 
		} 
		else 
		{ 
			if ($clen>=$start) 
			{
				$tmpstr.=substr($str,$i,1); 
				if ( $flag==1 )
				{
					$clen--;
					$flag=0;
				}
				else
				{
					$flag=1;
				}
				
			}
		} 
	}
	if ($i<$strlen) 
	$tmpstr.="..."; 
	
	return $tmpstr; 
} 

function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') 
	{ 
		if($code == 'UTF-8') 
		{ 
			$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; 
			preg_match_all($pa, $string, $t_string); 
	 
			if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."…"; 
			return join('', array_slice($t_string[0], $start, $sublen)); 
		} 
		else 
		{ 
			$start = $start*2; 
			$sublen = $sublen*2; 
			$strlen = strlen($string); 
			$tmpstr = ''; 
	 
			for($i=0; $i< $strlen; $i++) 
			{ 
				if($i>=$start && $i< ($start+$sublen)) 
				{ 
					if(ord(substr($string, $i, 1))>129) 
					{ 
						$tmpstr.= substr($string, $i, 2); 
					} 
					else 
					{ 
						$tmpstr.= substr($string, $i, 1); 
					} 
				} 
				if(ord(substr($string, $i, 1))>129) $i++; 
			} 
			if(strlen($tmpstr)< $strlen ) $tmpstr.= "…"; 
			return $tmpstr;
		} 
	} 

function isspage($total,$nownum,$psize,$page,$filename="",$parameter="")
{
	$isspage_str="";
	$isspage_str1="";
	$totalpage=ceil($total/$psize);
	$isspage_str.="页次<strong>{$page}/{$totalpage}</strong> &nbsp;每页<strong>{$psize}</strong>条 &nbsp;总数<strong>{$total}</strong>条 &nbsp; &nbsp;当前显示<strong>{$nownum}</strong>条";
	if ( $page>1 )
	{
		$isspage_str.='<a href="'.$filename.'?page=1'.$parameter.'">首页</a>&nbsp;<a href="'.$filename.'?page='.($page-1).$parameter.'">上一页</a>';
	}
	if ( $page<$totalpage )
	{
			$isspage_str.='&nbsp;<a href="'.$filename.'?page='.($page+1).$parameter.'">下一页</a>&nbsp;<a href="'.$filename.'?page='.$totalpage.$parameter.'">尾页</a>&nbsp;转到：';
	}
	
	if ($totalpage>1)
	{
		$isspage_str1='<select name="menu1" onChange="MM_jumpMenu(\'self\',this,0)">';
		for ($i=1;$i<=$totalpage;$i++)
		{
			if ($page==$i){
				$isspage_str1.='<option value="'.$filename.'?page='.$i.$parameter.'" selected="selected">第'.$i.'页</option>';
			}else{
				$isspage_str1.='<option value="'.$filename.'?page='.$i.$parameter.'">第'.$i.'页</option>';
			}
		}
		$isspage_str1.='</select>';
	}
	
	$str=array("str1"=>$isspage_str,"str2"=>$isspage_str1);
	return $str;
}

function GetIP(){//获取用户IP
	if(!empty($_SERVER["HTTP_CLIENT_IP"])) $cip = $_SERVER["HTTP_CLIENT_IP"];
	else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	else if(!empty($_SERVER["REMOTE_ADDR"])) $cip = $_SERVER["REMOTE_ADDR"];
	else $cip = "无法获取！";
	return $cip;
}

function getExtend_3($file_name)
{
	$extend=explode(".",$file_name);
	$va=count($extend)-1;
	return $extend[$va];
}//方法三

function alert_msg($msg="完成",$url="/index.php")
{
	if ($url=="-1"){
		echo '<meta http-equiv="Content-Type" content="text/html; charset=GB2312" /><script language="javascript">alert("'.$msg.'");history.back(-1);</script>';
	}else{
		echo '<meta http-equiv="Content-Type" content="text/html; charset=GB2312" /><script language="javascript">alert("'.$msg.'");location="'.$url.'";</script>';
	}
	exit();
}

function show_msg($msg,$url,$str){
	$option="";
	$i=1;
	foreach ($url as $k=>$v){
		$option.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$i.<a href='$v'>".$str[$k]."</a><br />";
		$i++;
	}
	echo '<table width="1003" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td valign="top"><table width="70%" height="59" border="0" align="center" cellpadding="0" cellspacing="1">
      <tr>
        <td height="30" background="/images/72.jpg"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="7%">&nbsp;</td>
            <td width="93%"><span class="white_14">提示信息</span></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td height="28"><table width="90%" border="0" align="center" cellpadding="0" cellspacing="0" class="kuang">
          <tr>
            <td height="15"><TABLE width="100%" border=0 align="center" cellPadding=5 cellSpacing=0>
                        
                          <TR>
                            <TD vAlign=center><table width="100%" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td bgcolor="#FFFFCC" height="30px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$msg.'</td>
                              </tr>
                              <tr>
                                <td bgcolor="#FFEDCE" height="30px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;您可以点击以下链接</td>
                              </tr>
                              <tr>
                                <td bgcolor="#FFFFCC" height="35px;">'.$option.'</td>
                              </tr>
                            </table></TD>
                          </TR>
                        
                    </TABLE></td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
</table>';
}

function post2answer($post){
	$answer=array();
	$cos=0;
	$row=0;
	foreach ($post as $k=>$v){
		if (substr($k,0,2)=="i_"){
			$u=explode("_",$k);
			$answer[$u[1]][$u[2]]=trim($v);
			$cos=($u[1]>$cos)?$u[1]:$cos;
			$row=($u[2]>$row)?$u[2]:$row;
		}
	}
	for($i=0;$i<=$cos;$i++){
		for($j=0;$j<=$row;$j++){
			if (!$answer[$i][$j]){
				$answer[$i][$j]="";
			}
		}
		ksort($answer[$i]);
	}
	return $answer;
}

function check_answered($gid){
	global $tjsql;
	if ($_SESSION["username"]){
		$sql="SELECT `id` FROM `#@__answer` WHERE `gid`='{$gid}' AND `uid`='".$_SESSION["uid"]."' LIMIT 0,1";
		$row=$tjsql->GetOne($sql);
		if ($row["id"]){
			alert_msg("对不起，你已经玩过这个游戏了","-1");
		}
	}
}

function get_web($url_full,$referer='',$out_set=''){
	global $str_header;
	$u=explode('\/',$url_full);
	$host=$u[2];
	for ($i=3;$i<count($u);$i++)
	$a[]=$u[$i];
	$file_url=implode('/',$a);
	$fp = fsockopen($host, 80, $errno, $errstr, 5);
	if (!$fp) {
		echo "$errstr ($errno)<br />\n";
	} else {
		$out = "GET /".$file_url." HTTP/1.1\r\n";
		$out .= "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*\r\n";
		if ($referer)$out .= "Referer: ".$referer."\r\n";
		$out .= "Accept-Language: zh-cn\r\n";
		$out .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n";
		$out .= "Host: ".$host."\r\n";
		$out .= "Connection: Close\r\n\r\n";
		if($out_set) $out=$out_set;
		fwrite($fp, $out);
		while (!feof($fp)) {
			$str.=fgets($fp,102400);
		}
		fclose($fp);
	}

	$u=explode("\r\n\r\n",$str);
	$str_header=strtolower($u[0]);
	$u[0]='';
	$str_body=trim(implode("\r\n",$u));
	if (!(strpos($str_header,strtolower('Transfer-Encoding: chunked'))===false)){
		$str_body=chunkdecode($str_body);
	} elseif (!(strpos($str_header,strtolower('Content-Encoding: gzip'))===false)){
		$str_body=gzdecode($str_body);
	}
	return $str_body;
}

function chunkdecode($str){
	$u=explode("\r\n",$str);
	$chunk_size = (integer)hexdec($u[0]);
	$str_t='';
	$str=substr($str,strlen($u[0])+2);
	while(strlen($str) && $chunk_size > 0) {
		$str_t.= substr($str,0,$chunk_size);
		$str=trim(substr($str,$chunk_size+2));
		$u=explode("\r\n",$str);
		$chunk_size = (integer)hexdec($u[0]);
		$str=substr($str,strlen($u[0])+2);
	}
	
	return $str_t;
}

function gzdecode ($data) {    
	$flags = ord(substr($data, 3, 1));    
	$headerlen = 10;    
	$extralen = 0;    
	$filenamelen = 0;    
	if ($flags & 4) {    
		$extralen = unpack('v' ,substr($data, 10, 2));    
		$extralen = $extralen[1];    
		$headerlen += 2 + $extralen;    
	}    
	if ($flags & 8) // Filename    
		$headerlen = strpos($data, chr(0), $headerlen) + 1;    
	if ($flags & 16) // Comment    
		$headerlen = strpos($data, chr(0), $headerlen) + 1;    
	if ($flags & 2) // CRC at end of file    
		$headerlen += 2;    
        $unpacked = @gzinflate(substr($data, $headerlen));    
	if ($unpacked === FALSE)    
		$unpacked = $data;    
	return $unpacked;    
}    

/******************************
//UTF-8 转GB编码
*******************************/
function utf82gb($utfstr)
{
	if(function_exists('iconv')){ return iconv('utf-8','gbk',$utfstr); }
	global $UC2GBTABLE;
	$okstr = "";
	if(trim($utfstr)=="") return $utfstr;
	if(empty($UC2GBTABLE)){
		$filename = dirname(__FILE__)."/data/gb2312-utf8.table";
		$fp = fopen($filename,"r");
		while($l = fgets($fp,15))
		{	$UC2GBTABLE[hexdec(substr($l, 7, 6))] = hexdec(substr($l, 0, 6));}
		fclose($fp);
	}
	$okstr = "";
	$ulen = strlen($utfstr);
	for($i=0;$i<$ulen;$i++)
	{
		$c = $utfstr[$i];
		$cb = decbin(ord($utfstr[$i]));
		if(strlen($cb)==8){ 
			$csize = strpos(decbin(ord($cb)),"0");
			for($j=0;$j < $csize;$j++){
				$i++; $c .= $utfstr[$i];
			}
			$c = utf82u($c);
			if(isset($UC2GBTABLE[$c])){
				$c = dechex($UC2GBTABLE[$c]+0x8080);
				$okstr .= chr(hexdec($c[0].$c[1])).chr(hexdec($c[2].$c[3]));
			}
			else
			{ $okstr .= "&#".$c.";";}
		}
		else $okstr .= $c;
	}
	$okstr = trim($okstr);
	return $okstr;
}
/*******************************
//GB转UTF-8编码
*******************************/
function gb2utf8($gbstr) {
	if(function_exists('iconv')){ return iconv('gbk','utf-8',$gbstr); }
	global $CODETABLE;
	if(trim($gbstr)=="") return $gbstr;
	if(empty($CODETABLE)){
		$filename = dirname(__FILE__)."/data/gb2312-utf8.table";
		$fp = fopen($filename,"r");
		while ($l = fgets($fp,15))
		{ $CODETABLE[hexdec(substr($l, 0, 6))] = substr($l, 7, 6); }
		fclose($fp);
	}
	$ret = "";
	$utf8 = "";
	while ($gbstr) {
		if (ord(substr($gbstr, 0, 1)) > 0x80) {
			$thisW = substr($gbstr, 0, 2);
			$gbstr = substr($gbstr, 2, strlen($gbstr));
			$utf8 = "";
			@$utf8 = u2utf8(hexdec($CODETABLE[hexdec(bin2hex($thisW)) - 0x8080]));
			if($utf8!=""){
				for ($i = 0;$i < strlen($utf8);$i += 3)
					$ret .= chr(substr($utf8, $i, 3));
			}
		}
		else
		{
			$ret .= substr($gbstr, 0, 1);
			$gbstr = substr($gbstr, 1, strlen($gbstr));
		}
	}
	return $ret;
}
//Unicode转utf8
function u2utf8($c) {
	for ($i = 0;$i < count($c);$i++)
		$str = "";
	if ($c < 0x80) {
		$str .= $c;
	} else if ($c < 0x800) {
		$str .= (0xC0 | $c >> 6);
		$str .= (0x80 | $c & 0x3F);
	} else if ($c < 0x10000) {
		$str .= (0xE0 | $c >> 12);
		$str .= (0x80 | $c >> 6 & 0x3F);
		$str .= (0x80 | $c & 0x3F);
	} else if ($c < 0x200000) {
		$str .= (0xF0 | $c >> 18);
		$str .= (0x80 | $c >> 12 & 0x3F);
		$str .= (0x80 | $c >> 6 & 0x3F);
		$str .= (0x80 | $c & 0x3F);
	}
	return $str;
}
//utf8转Unicode
function utf82u($c)
{
  switch(strlen($c)) {
    case 1:
      return ord($c);
    case 2:
      $n = (ord($c[0]) & 0x3f) << 6;
      $n += ord($c[1]) & 0x3f;
      return $n;
    case 3:
      $n = (ord($c[0]) & 0x1f) << 12;
      $n += (ord($c[1]) & 0x3f) << 6;
      $n += ord($c[2]) & 0x3f;
      return $n;
    case 4:
      $n = (ord($c[0]) & 0x0f) << 18;
      $n += (ord($c[1]) & 0x3f) << 12;
      $n += (ord($c[2]) & 0x3f) << 6;
      $n += ord($c[3]) & 0x3f;
      return $n;
  }
}
/**********************************
//Big5码转换成GB码
**********************************/
function big52gb($Text) {
	if(function_exists('iconv')){ return iconv('big5','gbk',$Text); }
	global $BIG5_DATA;
	if(empty($BIG5_DATA)){
		$filename = dirname(__FILE__)."/data/big5-gb.table";
		$fp = fopen($filename, "rb");
		$BIG5_DATA = fread($fp,filesize($filename));
		fclose($fp);
	}
	$max = strlen($Text)-1;
	for($i=0;$i<$max;$i++) {
		$h = ord($Text[$i]);
		if($h>=0x80) {
			$l = ord($Text[$i+1]);
			if($h==161 && $l==64) {
					$gbstr = "　";
			}else{
					$p = ($h-160)*510+($l-1)*2;
					$gbstr = $BIG5_DATA[$p].$BIG5_DATA[$p+1];
			}
			$Text[$i] = $gbstr[0];
			$Text[$i+1] = $gbstr[1];
			$i++;
		}
	}
	return $Text;
}
/********************************
//GB码转换成Big5码
*********************************/
function gb2big5($Text) {
	if(function_exists('iconv')){ return iconv('gbk','big5',$Text); }
	global $GB_DATA;
	if(empty($GB_DATA)){
		$filename = dirname(__FILE__)."/data/gb-big5.table";
		$fp = fopen($filename, "rb");
		$gb = fread($fp,filesize($filename));
		fclose($fp);
	}
	$max = strlen($Text)-1;
	for($i=0;$i<$max;$i++) {
		$h = ord($Text[$i]);
		if($h>=0x80) {
			$l = ord($Text[$i+1]);
			if($h==161 && $l==64) {
			$big = "　";
			}else{
				$p = ($h-160)*510+($l-1)*2;
				$big = $GB_DATA[$p].$GB_DATA[$p+1];
			}
			$Text[$i] = $big[0];
			$Text[$i+1] = $big[1];
			$i++;
		}
	}
	return $Text;
}

function getProcessedTime(){
	$mtime = explode(' ', microtime());
	$starttime = $mtime[0] + $mtime[1];
	return $starttime;
}

//-------------运行时间显示，一般在程序结尾---------------
function showProcessedTime($starttime,$debug=""){
	$mtime = explode(' ', microtime());
	$totaltime = number_format(($mtime[0] + $mtime[1] - $starttime), 6);
	echo "$debug Processed in <b>".$totaltime."</b> second(s)<br>";
}

function GetCkVdValue(){
	@session_start();
	if(isset($_SESSION["dd_ckstr"])) $ckvalue = $_SESSION["dd_ckstr"];
	else $ckvalue = '';
	return $ckvalue;
}

function DropCkVdValue(){
	@session_start();
	if(isset($_SESSION["dd_ckstr"])) $_SESSION["dd_ckstr"]=rand(1000,9999);
	return true;
}

function get_config($key){
	global $tjsql;
	$sql="SELECT `value` FROM `#@__config` WHERE `key`='{$key}' LIMIT 0,1";
	$row=$tjsql->GetOne($sql);
	return $row["value"];
}

function set_config($key,$value){
	global $tjsql;
	$sql="REPLACE INTO `#@__config` (`key`,`value`) VALUES ('{$key}','{$value}')";
	$rs=$tjsql->ExecuteNoneQuery2($sql);
	return $rs;
}

function num2str($num){//  0-100
	global $cfg_num_str;
	if (!is_numeric($num)) return false;
	if ($num<=10){
		return $cfg_num_str[$num];
	}else{
		$ten=floor($num/10);
		if ($ten==1) $ten="十";
		else $ten=$cfg_num_str[$ten]."十";
		$sin=$num%10;
		if ($sin==0) $sin="";
		else $sin=$cfg_num_str[$sin];
		return $ten.$sin;
	}
}

function convertip_tiny($ip) {
	$ipdatafile=dirname(__FILE__)."/data/tinyipdata.dat";
	static $fp = NULL, $offset = array(), $index = NULL;

	$ipdot = explode('.', $ip);
	$ip    = pack('N', ip2long($ip));

	$ipdot[0] = (int)$ipdot[0];
	$ipdot[1] = (int)$ipdot[1];

	if($fp === NULL && $fp = @fopen($ipdatafile, 'rb')) {
		$offset = unpack('Nlen', fread($fp, 4));
		$index  = fread($fp, $offset['len'] - 4);
	} elseif($fp == FALSE) {
		return  '- Invalid IP data file';
	}

	$length = $offset['len'] - 1028;
	$start  = unpack('Vlen', $index[$ipdot[0] * 4] . $index[$ipdot[0] * 4 + 1] . $index[$ipdot[0] * 4 + 2] . $index[$ipdot[0] * 4 + 3]);

	for ($start = $start['len'] * 8 + 1024; $start < $length; $start += 8) {

		if ($index{$start} . $index{$start + 1} . $index{$start + 2} . $index{$start + 3} >= $ip) {
			$index_offset = unpack('Vlen', $index{$start + 4} . $index{$start + 5} . $index{$start + 6} . "\x0");
			$index_length = unpack('Clen', $index{$start + 7});
			break;
		}
	}

	fseek($fp, $offset['len'] + $index_offset['len'] - 1024);
	if($index_length['len']) {
		return '- '.fread($fp, $index_length['len']);
	} else {
		return '- Unknown';
	}

}

function convertip_full($ip) {
	$ipdatafile=dirname(__FILE__)."/data/tinyipdata.dat";
	
	if(!$fd = @fopen($ipdatafile)) {
		return '- Invalid IP data file';
	}

	$ip = explode('.', $ip);
	$ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];

	if(!($DataBegin = fread($fd, 4)) || !($DataEnd = fread($fd, 4)) ) return;
	@$ipbegin = implode('', unpack('L', $DataBegin));
	if($ipbegin < 0) $ipbegin += pow(2, 32);
	@$ipend = implode('', unpack('L', $DataEnd));
	if($ipend < 0) $ipend += pow(2, 32);
	$ipAllNum = ($ipend - $ipbegin) / 7 + 1;

	$BeginNum = $ip2num = $ip1num = 0;
	$ipAddr1 = $ipAddr2 = '';
	$EndNum = $ipAllNum;

	while($ip1num > $ipNum || $ip2num < $ipNum) {
		$Middle= intval(($EndNum + $BeginNum) / 2);

		fseek($fd, $ipbegin + 7 * $Middle);
		$ipData1 = fread($fd, 4);
		if(strlen($ipData1) < 4) {
			fclose($fd);
			return '- System Error';
		}
		$ip1num = implode('', unpack('L', $ipData1));
		if($ip1num < 0) $ip1num += pow(2, 32);

		if($ip1num > $ipNum) {
			$EndNum = $Middle;
			continue;
		}

		$DataSeek = fread($fd, 3);
		if(strlen($DataSeek) < 3) {
			fclose($fd);
			return '- System Error';
		}
		$DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
		fseek($fd, $DataSeek);
		$ipData2 = fread($fd, 4);
		if(strlen($ipData2) < 4) {
			fclose($fd);
			return '- System Error';
		}
		$ip2num = implode('', unpack('L', $ipData2));
		if($ip2num < 0) $ip2num += pow(2, 32);

		if($ip2num < $ipNum) {
			if($Middle == $BeginNum) {
				fclose($fd);
				return '- Unknown';
			}
			$BeginNum = $Middle;
		}
	}

	$ipFlag = fread($fd, 1);
	if($ipFlag == chr(1)) {
		$ipSeek = fread($fd, 3);
		if(strlen($ipSeek) < 3) {
			fclose($fd);
			return '- System Error';
		}
		$ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
		fseek($fd, $ipSeek);
		$ipFlag = fread($fd, 1);
	}

	if($ipFlag == chr(2)) {
		$AddrSeek = fread($fd, 3);
		if(strlen($AddrSeek) < 3) {
			fclose($fd);
			return '- System Error';
		}
		$ipFlag = fread($fd, 1);
		if($ipFlag == chr(2)) {
			$AddrSeek2 = fread($fd, 3);
			if(strlen($AddrSeek2) < 3) {
				fclose($fd);
				return '- System Error';
			}
			$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
			fseek($fd, $AddrSeek2);
		} else {
			fseek($fd, -1, SEEK_CUR);
		}

		while(($char = fread($fd, 1)) != chr(0))
		$ipAddr2 .= $char;

		$AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
		fseek($fd, $AddrSeek);

		while(($char = fread($fd, 1)) != chr(0))
		$ipAddr1 .= $char;
	} else {
		fseek($fd, -1, SEEK_CUR);
		while(($char = fread($fd, 1)) != chr(0))
		$ipAddr1 .= $char;

		$ipFlag = fread($fd, 1);
		if($ipFlag == chr(2)) {
			$AddrSeek2 = fread($fd, 3);
			if(strlen($AddrSeek2) < 3) {
				fclose($fd);
				return '- System Error';
			}
			$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
			fseek($fd, $AddrSeek2);
		} else {
			fseek($fd, -1, SEEK_CUR);
		}
		while(($char = fread($fd, 1)) != chr(0))
		$ipAddr2 .= $char;
	}
	fclose($fd);

	if(preg_match('/http/i', $ipAddr2)) {
		$ipAddr2 = '';
	}
	$ipaddr = "$ipAddr1 $ipAddr2";
	$ipaddr = preg_replace('/CZ88\.NET/is', '', $ipaddr);
	$ipaddr = preg_replace('/^\s*/is', '', $ipaddr);
	$ipaddr = preg_replace('/\s*$/is', '', $ipaddr);
	if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
		$ipaddr = '- Unknown';
	}

	return '- '.$ipaddr;
}

function del_file($file){
	if (file_exists($file)){
		if (is_dir($file)){
			clear_dir($file);
			unlink($file);
		}else{
			unlink($file);
		}
	}
	return false;
}

function clear_dir($dir){
	if (file_exists($dir) && is_dir($dir)){
		$handle=opendir($dir);
		while ($file=readdir($handle)) {
			if ($file!="." && $file!="..") del_file($dir."/".$file);
		}
	}
	return false;
}

function show_ad($ad,$width,$height){
	$file=$ad['img'];
	$link=$ad['link'];
	$str=$ad['text'];
	if (!getExtend_3($file) || !file_exists(dirname(__FILE__)."/../upload/".$file)){
		return '';
	}
	if (getExtend_3($file)=='swf'){
		$rs='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'.$width.'" height="'.$height.'">
	              <param name="movie" value="/upload/'.$file.'" />
	              <param name="quality" value="high" />
	              <param name="wmode" value="transparent" />
	              <embed   src="/upload/'.$file.'" quality="high" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'.$width.'" height="'.$height.'"></embed>
	    </object>';
	}else {
		$rs='<a href="'.$link.'" target="_blank" title="'.$str.'"><img src="/upload/'.$file.'" width="'.$width.'" height="'.$height.'" alt="'.$str.'"></a>';
	}
	
	return $rs;
}
?>